function collData(obj){
	var data = {};
	if(typeof obj == 'object'){
		//文本框
		$(obj).find("input[type='text']").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//密码框
		$(obj).find("input[type='password']").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//隐藏域
		$(obj).find("input[type='hidden']").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//select的值
		$(obj).find("select").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//console.log(data);
	}
	return data;
}
//key event then move position
function moveFocus($obj){
	if($obj.get(0).tagName != 'TABLE') return;
	$obj.find("input[type='text']").keyup(function(e){
		var key_code = e.which;
		var $crrRow = $(this).parent().parent(); //current row
		var crrC = $(this).parent().index();;	//current column index

		if(key_code==38){	//up
			$crrRow.prev().children("td:eq(" + crrC + ")").children("input").select();
		}else if(key_code==40){	//down
			$crrRow.next().children("td:eq(" + crrC + ")").children("input").select();
		}else if(key_code==37){	//left
			$crrRow.children("td:eq(" + crrC + ")").prev().children("input").select();
		}else if(key_code==39){	//right
			$crrRow.children("td:eq(" + crrC + ")").next().children("input").select();
		}else if(key_code==13){	//enter
			$crrRow.next().children("td:eq(" + crrC + ")").children("input").select();
		}
	});
}
function getFormData(form_id){
	//alert();
	var data = {};
	if(typeof $(form_id) == 'object'){
		//文本框
		$(form_id).find("input[type='text']").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//密码框
		$(form_id).find("input[type='password']").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//隐藏域
		$(form_id).find("input[type='hidden']").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//select的值
		$(form_id).find("select").each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		//console.log(data);
	}
	return data;
}
var curPage;        //当前页数
var pageSize=1;       //每一页记录数
var totalPage;      //总页数
 
//获取分页数据
function turnPage(url,search_form_id,selector,page=1)
{
	var data = {};
	data['page'] = page;
	data['condition'] = getFormData(search_form_id);
  	$.ajax({
	    type: 'POST',
	    url: url,     //这里是请求的后台地址，自己定义
	    data: data,
	    dataType: 'json',
	    beforeSend: function() {
	    	$('#tips').show();
	    },
	    success: function(rsp) {
	    	$(selector).empty();       //移除原来的分页数据
	    	totalPage = Math.ceil(rsp.total/pageSize);
	    	//var data_content = json.data_content;
	    	var data_html = template('template',rsp);
 
	    	$(selector).html(data_html);
	    },
	    complete: function() {    //添加分页按钮栏
	    	$('#tips').hide();
	    	$("#pageBar").pager({
		      	pagenumber: page, 
		      	pagecount: totalPage, 
		      	buttonClickCallback: function(i) {
		      		turnPage(url,search_form_id,selector,i)
		      	}
	      	});
	    },
	    error: function() {
	      	alert("数据加载失败");
	    }
  	});
}
 
//获取分页条（分页按钮栏的规则和样式根据自己的需要来设置）
function getPageBar()
{
  if(curPage > totalPage) {
    curPage = totalPage;
  }
  if(curPage < 1) {
    curPage = 1;
  }
 
  pageBar = "";
 
  //如果不是第一页
  if(curPage != 1){
    pageBar += "<span class='pageBtn'><a href='javascript:void(1)'>首页</a></span>";
    pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+(curPage-1)+")'><<</a></span>";
  }
 
  //显示的页码按钮(5个)
  var start,end;
  if(totalPage <= 5) {
    start = 1;
    end = totalPage;
  } else {
    if(curPage-2 <= 0) {
        start = 1;
        end = 5;
    } else {
        if(totalPage-curPage < 2) {
            start = totalPage - 4;
            end = totalPage;
        } else {
            start = curPage - 2;
            end = curPage + 2;
        }
    }
  }
 
  for(var i=start;i<=end;i++) {
    if(i == curPage) {
        pageBar += "<span class='pageBtn-selected'><a href='javascript:turnPage("+i+")'>"+i+"</a></span>";
    } else {
        pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+i+")'>"+i+"</a></span>";
    }
  }
   
  //如果不是最后页
  if(curPage != totalPage){
    pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+(parseInt(curPage)+1)+")'>>></a></span>";
    pageBar += "<span class='pageBtn'><a href='javascript:turnPage("+totalPage+")'>尾页</a></span>";
  }
     
  $("#pageBar").html(pageBar);
}